﻿using IntoStationSystem.Entity;
using IntoStationSystem.Global;
using IntoStationSystem.Models;
using IntoStationSystem.ViewModels;
using Newtonsoft.Json;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Migrations;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;

namespace IntoStationSystem.Db
{
    internal class DbHelper
    {
        static object locker = new object();
        #region Local
        public static int LogProductLocal(ProduceData p, string workstation = null)
        {
            int result = -1;
            lock (locker)
            {
                try
                {
                    using (ConfigDbContext db = new ConfigDbContext())
                    {
                        var mp = db.LogProduct.Where(x => x.TrayId == p.TrayId).FirstOrDefault();
                        if (mp == null)
                        {
                            mp = new ProduceData();
                        }
                        mp.TrayId = p.TrayId;
                        mp.DateTime = p.DateTime;
                        if (string.IsNullOrEmpty(mp.ProductMode))
                        {
                            mp.ProductMode = p.ProductMode;
                        }
                        mp.Result = p.Result;
                        mp.IsRepair = p.IsRepair;
                        mp.LastStationData = p.LastStationData;
                        #region
                        //Type SetType = mp.GetType();
                        //Type getType = p.GetType();
                        //// 获取所有公共实例属性（包括继承的）
                        //PropertyInfo[] SetProperties = SetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        //PropertyInfo[] getProperties = getType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        //foreach (PropertyInfo property in SetProperties)
                        //{
                        //    foreach (PropertyInfo property2 in getProperties)
                        //    {
                        //        if (property2.Name == property.Name && property2.Name.Contains(workstation))
                        //        {
                        //            property.SetValue(mp, property2.GetValue(SetType));
                        //            break;
                        //        }
                        //    }
                        //}
                        #endregion
                        #region
                        switch (workstation)
                        {
                            case "OP10":
                                mp.OP10PartCode1 = p.OP10PartCode1;
                                mp.OP10PartCode2 = p.OP10PartCode2;
                                mp.OP10PartCode3 = p.OP10PartCode3;
                                mp.OP10PartCode4 = p.OP10PartCode4;
                                mp.OP10Angle1 = p.OP10Angle1;
                                mp.OP10Angle2 = p.OP10Angle2;
                                mp.OP10Angle3 = p.OP10Angle3;
                                mp.OP10Angle4 = p.OP10Angle4;
                                mp.OP10Torque1 = p.OP10Torque1;
                                mp.OP10Torque2 = p.OP10Torque2;
                                mp.OP10Torque3 = p.OP10Torque3;
                                mp.OP10Torque4 = p.OP10Torque4;
                                mp.OP10Result = p.OP10Result;
                                break;
                            case "OP20":
                                mp.OP20PartCode1 = p.OP20PartCode1;
                                mp.OP20PartCode2 = p.OP20PartCode2;
                                mp.OP20PartCode3 = p.OP20PartCode3;
                                mp.OP20PartCode4 = p.OP20PartCode4;
                                mp.OP20Angle1 = p.OP20Angle1;
                                mp.OP20Angle2 = p.OP20Angle2;
                                mp.OP20Torque1 = p.OP20Torque1;
                                mp.OP20Torque2 = p.OP20Torque2;
                                mp.OP20Image = p.OP20Image;
                                mp.OP20Result = p.OP20Result;
                                break;
                            case "OP30":
                                mp.OP30PartCode1 = p.OP30PartCode1;
                                mp.OP30PartCode2 = p.OP30PartCode2;
                                mp.OP30PartCode3 = p.OP30PartCode3;
                                mp.OP30PartCode4 = p.OP30PartCode4;
                                mp.OP30Angle1 = p.OP30Angle1;
                                mp.OP30Angle2 = p.OP30Angle2;
                                mp.OP30Angle3 = p.OP30Angle3;
                                mp.OP30Angle4 = p.OP30Angle4;
                                mp.OP30Torque1 = p.OP30Torque1;
                                mp.OP30Torque2 = p.OP30Torque2;
                                mp.OP30Torque3 = p.OP30Torque3;
                                mp.OP30Torque4 = p.OP30Torque4;
                                mp.OP30Result = p.OP30Result;
                                break;
                            case "OP40":

                                break;
                            case "OP50":
                                mp.OP50Voltage1 = p.OP50Voltage1;
                                mp.OP50Current1 = p.OP50Current1;
                                mp.OP50Frequency1 = p.OP50Frequency1;
                                mp.OP50Duty1 = p.OP50Duty1;
                                mp.OP50FanResult1 = p.OP50FanResult1;
                                mp.OP50Voltage2 = p.OP50Voltage2;
                                mp.OP50Current2 = p.OP50Current2;
                                mp.OP50Frequency2 = p.OP50Frequency2;
                                mp.OP50Duty2 = p.OP50Duty2;
                                mp.OP50FanResult2 = p.OP50FanResult2;
                                mp.OP50Presure = p.OP50Presure;
                                mp.OP50Leakage = p.OP50Leakage;
                                mp.OP50GasResult = p.OP50GasResult;
                                mp.OP50Result = p.OP50Result;
                                break;
                            case "OP60":
                                mp.OP60Code = p.OP60Code;
                                mp.OP60Result = p.OP60Result;
                                mp.OP60Picture = p.OP60Picture;
                                break;
                            case "GP12": //GP12工位
                                mp.GP12Result = p.GP12Result;
                                mp.GP12Image1 = p.GP12Image1;
                                mp.GP12Image2 = p.GP12Image2;
                                break;
                            case "JZ": // 减震工位
                                mp.JZResult = p.JZResult;
                                mp.JZImage1 = p.JZImage1;
                                mp.JZImage2 = p.JZImage2;
                                break;
                        }
                        #endregion
                        db.LogProduct.AddOrUpdate(mp);
                        result = db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    DbHelper.LogTip($"{DateTime.Now.ToString("HH:mm:ss:fff")} : {e.Message}");
                }
            }
            return result;
        }
        /// <summary>
        /// 获取产品配置名称
        /// </summary>
        /// <returns></returns>
        public static List<string> GetProductNames()
        {
            List<string> result;
            lock (locker)
            {
                using (ConfigDbContext db = new ConfigDbContext())
                {
                    result = db.ProductModels.Select(o => o.Name).Distinct().ToList();
                }
            }
            return result;
        }

        /// <summary>
        /// 判断扫码是否重复
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ContainsPartCode(string code)
        {
            bool flag = false;
            lock (locker)
            {
                try
                {
                    using (ConfigDbContext db = new ConfigDbContext())
                    {
                        var query = db.PartCodesQuery.Where(o => o.Code == code).ToList();

                        if (query.Count() > 0)
                        {
                            //获取最近一次扫码的数据，判断是否解绑,没解绑则提示条码重复
                            var data = query.OrderByDescending(t => t.DateTime).First();
                            if (!data.IsReset)
                            {
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            PartCodeModel model = new PartCodeModel();
                            model.DateTime = DateTime.Now;
                            model.Code = code;
                            model.IsReset = false;
                            db.PartCodesQuery.Add(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch { }
            }
            return flag;
        }

        /// <summary>
        /// 获取数据库的扫码信息
        /// </summary>
        /// <param name="repeatCode"></param>
        /// <returns></returns>
        public static List<PartCodeModel> LoadPartCodes(string repeatCode)
        {
            List<PartCodeModel> codes = null;
            lock (locker)
            {
                try
                {
                    using (ConfigDbContext db = new ConfigDbContext())
                    {
                        codes = db.PartCodesQuery.Where(o => o.Code == repeatCode&o.IsReset==false).ToList();
                    }
                }
                catch { }
            }
            return codes;
        }


        /// <summary>
        /// 修改是否解绑
        /// </summary>
        /// <param name="list"></param>
        public static void ResetPartCode(ObservableCollection<ResetPartCodeModel> list)
        {
            lock (locker)
            {
                try
                {
                    using (ConfigDbContext db = new ConfigDbContext())
                    {
                        foreach (var model in list)
                        {
                            db.PartCodesQuery.Where(o => o.Count == model.Index).FirstOrDefault().IsReset = model.IsReset;
                        }
                        db.SaveChanges();
                    }
                }
                catch { }
            }
        }



        /// <summary>
        /// 获取配置参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<T> GetConfig<T>(string name = null)
        {
            object result = null;
            Type type = typeof(T);
            if (string.IsNullOrEmpty(name))
            {
                name = "default";
            }
            lock (locker)
            {
                using (ConfigDbContext db = new ConfigDbContext())
                {
                    switch (type.Name)
                    {
                        case "Cylinder":
                            result = db.Cylinders.ToList();
                            break;
                        case "IO":
                            result = db.IO.ToList();
                            break;
                        case "ModelIndex":
                            result = db.ModelIndexs.ToList();
                            break;
                        case "ProductModel":
                            result = db.ProductModels.Where(o => o.Name == name).ToList();
                            break;
                        case "ScanCode":
                            result = db.ScanCodes.Where(o => o.ProductName == name).ToList();
                            break;
                    }
                }
            }
            return (List<T>)result;
        }
        /// <summary>
        /// 设置默认配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void SetDefault<T>()
        {
            Type type = typeof(T);
            lock (locker)
            {
                using (ConfigDbContext db = new ConfigDbContext())
                {
                    switch (type.Name)
                    {
                        case "Cylinder":
                            for (int i = 0; i < Fields.CylinderCount; i++)
                            {
                                db.Cylinders.Add(new Cylinder() { Count = i + 1, Name = "备用" });
                            }
                            db.Cylinders.AddOrUpdate();
                            break;
                        case "IO":
                            for (int i = 0; i < Fields.IOCount; i++)
                            {
                                db.IO.Add(new IO() { Count = i + 1, Type = "I", Addr = "I10,0", Desc = $"Test{i + 1}" });
                                db.IO.Add(new IO() { Count = i + 1, Type = "O", Addr = "I10,0", Desc = $"Test{i + 1}" });
                            }
                            db.IO.AddOrUpdate();
                            break;
                        case "ProductModel":
                            for (int i = 0; i < Fields.ProductParaCount; i++)
                            {
                                db.ProductModels.Add(new ProductModel() { Name = "default", Index = i + 1, RealValue = 1.1f, RealDesc = $"Test{i + 1}" });
                            }
                            db.ProductModels.AddOrUpdate();
                            break;
                        case "ScanCode":
                            for (int i = 0; i < Fields.ScanCount; i++)
                            {
                                db.ScanCodes.Add(new ScanCode() { ProductName = "default", Enable = false, PartName = $"零件名称{i + 1}", CharLen = 10, MatchChar = "111" });
                            }
                            db.ScanCodes.AddOrUpdate();
                            break;
                        case "ModelIndex":
                           
                            db.ModelIndexs.Add(new ModelIndex() { Name = "昆仑X",Index=1});
                            db.ModelIndexs.Add(new ModelIndex() { Name = "MS11", Index =2 });
                            db.ModelIndexs.AddOrUpdate();
                            break;
                    }
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// 保存配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        public static void SaveConfig<T>(List<T> list, string proName)
        {
            Type type = typeof(T);
            object cache = list;
            lock (locker)
            {
                using (ConfigDbContext db = new ConfigDbContext())
                {
                    switch (type.Name)
                    {
                        case "ProductModel":
                            var oldProConfig = db.ProductModels.Where(o => o.Name == proName).ToList();
                            if (oldProConfig != null)
                            {
                                db.ProductModels.RemoveRange(oldProConfig);
                            }
                            List<ProductModel> pros = (List<ProductModel>)cache;
                            db.ProductModels.AddRange(pros);
                            db.ProductModels.AddOrUpdate();
                            break;
                        case "ScanCode":
                            var oldScanConfig = db.ScanCodes.Where(o => o.ProductName == proName).ToList();
                            if (oldScanConfig != null)
                            {
                                db.ScanCodes.RemoveRange(oldScanConfig);
                            }
                            List<ScanCode> scans = (List<ScanCode>)cache;
                            db.ScanCodes.AddRange(scans);
                            db.ScanCodes.AddOrUpdate();
                            break;
                        case "ModelIndex":
                            var result = db.ModelIndexs.ToList();
                            db.ModelIndexs.RemoveRange(result);
                            List<ModelIndex> modelIndex = (List<ModelIndex>)cache;
                            db.ModelIndexs.AddRange(modelIndex);
                            db.ModelIndexs.AddOrUpdate();
                            break;
                    }
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// 设置IO配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="SingleType"></param>
        public static void SetIOConfig<T>(List<T> list, string SingleType)
        {
            Type type = typeof(T);
            object cache = list;
            using (ConfigDbContext db = new ConfigDbContext())
            {
                var oldIOConfig = db.IO.ToList();
                if (oldIOConfig != null)
                {
                    db.IO.RemoveRange(oldIOConfig);
                }
                List<IO> IOModels = (List<IO>)cache;
                db.IO.AddRange(IOModels);
                db.IO.AddOrUpdate();
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 删除配置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="proName"></param>
        public static void DeleteConfig<T>(string proName)
        {
            Type type = typeof(T);
            lock (locker)
            {
                using (ConfigDbContext db = new ConfigDbContext())
                {
                    switch (type.Name)
                    {
                        case "ProductModel":
                            var oldProConfig = db.ProductModels.Where(o => o.Name == proName).ToList();
                            if (oldProConfig != null)
                            {
                                db.ProductModels.RemoveRange(oldProConfig);
                            }
                            break;
                        case "ScanCodes":
                            var oldScanConfig = db.ScanCodes.Where(o => o.ProductName == proName).ToList();
                            if (oldScanConfig != null)
                            {
                                db.ScanCodes.RemoveRange(oldScanConfig);
                            }
                            break;
                        case "ModelIndex":
                            var oldModelIndexConfig = db.ModelIndexs.Where(o => o.Name == proName).ToList();
                            if (oldModelIndexConfig != null)
                            {
                                db.ModelIndexs.RemoveRange(oldModelIndexConfig);
                            }
                            break;
                    }
                    db.SaveChanges();
                }
            }
        }
        #endregion

        #region Remote
        /// <summary>
        /// 更新或者添加产品
        /// </summary>
        /// <param name="identity"></param>
        public static int LogProductRemote(ProduceData p, string workstation = null)
        {
            int result = -1;
            lock (locker)
            {
                try
                {
                    using (ProductionQueryContext db = new ProductionQueryContext())
                    {
                        var mp = db.Production.Where(x => x.TrayId == p.TrayId.Trim()).FirstOrDefault();
                        if (mp == null)
                        {
                            mp = new ProduceData();
                            mp.IsRepair = p.IsRepair;
                        }
                        mp.TrayId = p.TrayId;
                        mp.DateTime = p.DateTime;
                        if (string.IsNullOrEmpty(mp.ProductMode))
                        {
                            mp.ProductMode = p.ProductMode;
                        }
                        mp.Result = p.Result;
                        
                        mp.LastStationData = p.LastStationData;

                        #region
                        //Type SetType = mp.GetType();
                        //Type getType = p.GetType();
                        //// 获取所有公共实例属性（包括继承的）
                        //PropertyInfo[] SetProperties = SetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        //PropertyInfo[] getProperties = getType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                        //foreach (PropertyInfo property in SetProperties)
                        //{
                        //    foreach (PropertyInfo property2 in getProperties)
                        //    {
                        //        if (property2.Name == property.Name && property2.Name.Contains(workstation))
                        //        {
                        //            property.SetValue(mp, property2.GetValue(SetType));
                        //            break;
                        //        }
                        //    }
                        //}
                        #endregion
                        #region
                        switch (workstation)
                        {
                            case "OP10":
                                mp.OP10PartCode1 = p.OP10PartCode1;
                                mp.OP10PartCode2 = p.OP10PartCode2;
                                mp.OP10PartCode3 = p.OP10PartCode3;
                                mp.OP10PartCode4 = p.OP10PartCode4;
                                mp.OP10Angle1 = p.OP10Angle1;
                                mp.OP10Angle2 = p.OP10Angle2;
                                mp.OP10Angle3 = p.OP10Angle3;
                                mp.OP10Angle4 = p.OP10Angle4;
                                mp.OP10Torque1 = p.OP10Torque1;
                                mp.OP10Torque2 = p.OP10Torque2;
                                mp.OP10Torque3 = p.OP10Torque3;
                                mp.OP10Torque4 = p.OP10Torque4;
                                mp.OP10Result = p.OP10Result;
                                break;
                            case "OP20":
                                mp.OP20PartCode1 = p.OP20PartCode1;
                                mp.OP20PartCode2 = p.OP20PartCode2;
                                mp.OP20PartCode3 = p.OP20PartCode3;
                                mp.OP20PartCode4 = p.OP20PartCode4;
                                mp.OP20Angle1 = p.OP20Angle1;
                                mp.OP20Angle2 = p.OP20Angle2;
                                mp.OP20Torque1 = p.OP20Torque1;
                                mp.OP20Torque2 = p.OP20Torque2;
                                mp.OP20Result = p.OP20Result;
                                mp.OP20Image = p.OP20Image;
                                break;
                            case "OP30":
                                mp.OP30PartCode1 = p.OP30PartCode1;
                                mp.OP30PartCode2 = p.OP30PartCode2;
                                mp.OP30PartCode3 = p.OP30PartCode3;
                                mp.OP30PartCode4 = p.OP30PartCode4;
                                mp.OP30Angle1 = p.OP30Angle1;
                                mp.OP30Angle2 = p.OP30Angle2;
                                mp.OP30Angle3 = p.OP30Angle3;
                                mp.OP30Angle4 = p.OP30Angle4;
                                mp.OP30Torque1 = p.OP30Torque1;
                                mp.OP30Torque2 = p.OP30Torque2;
                                mp.OP30Torque3 = p.OP30Torque3;
                                mp.OP30Torque4 = p.OP30Torque4;
                                mp.OP30Result = p.OP30Result;
                                break;
                            case "OP40":
                                mp.OP10PartCode1 = p.OP10PartCode1;
                                mp.OP10PartCode2 = p.OP10PartCode2;
                                mp.OP10PartCode3 = p.OP10PartCode3;
                                mp.OP10PartCode4 = p.OP10PartCode4;
                                mp.OP10Angle1 =    p.OP10Angle1;
                                mp.OP10Torque1 =   p.OP10Torque1;
                                mp.OP10Angle2 =    p.OP10Angle2;
                                mp.OP10Torque2 =   p.OP10Torque2;
                                mp.OP10Angle3 =    p.OP10Angle3;
                                mp.OP10Torque3 =   p.OP10Torque3;
                                mp.OP10Angle4 =    p.OP10Angle4;
                                mp.OP10Torque4 =   p.OP10Torque4;
                                mp.OP20PartCode1 = p.OP20PartCode1;
                                mp.OP20PartCode2 = p.OP20PartCode2;
                                mp.OP20PartCode3 = p.OP20PartCode3;
                                mp.OP20PartCode4 = p.OP20PartCode4;
                                mp.OP20Angle1 =    p.OP20Angle1;
                                mp.OP20Torque1 =   p.OP20Torque1;
                                mp.OP20Angle2 =    p.OP20Angle2;
                                mp.OP20Torque2 =   p.OP20Torque2;
                                mp.OP20Angle3 =    p.OP20Angle3;
                                mp.OP20Torque3 =   p.OP20Torque3;
                                mp.OP20Angle4 =    p.OP20Angle4;
                                mp.OP20Torque4 =   p.OP20Torque4;
                                mp.OP20Image =     p.OP20Image;
                                mp.OP30PartCode1 = p.OP30PartCode1;
                                mp.OP30PartCode2 = p.OP30PartCode2;
                                mp.OP30PartCode3 = p.OP30PartCode3;
                                mp.OP30PartCode4 = p.OP30PartCode4;
                                mp.OP30Angle1 =    p.OP30Angle1;
                                mp.OP30Torque1 =   p.OP30Torque1;
                                mp.OP30Angle2 =    p.OP30Angle2;
                                mp.OP30Torque2 =   p.OP30Torque2;
                                mp.OP30Angle3 =    p.OP30Angle3;
                                mp.OP30Torque3 =   p.OP30Torque3;
                                mp.OP30Angle4 =    p.OP30Angle4;
                                mp.OP30Torque4 =   p.OP30Torque4;
                                mp.OP10Result = "OK";
                                mp.OP20Result = "OK";
                                mp.OP30Result = "OK";
                                break;
                            case "OP50":
                                mp.OP50Voltage1 = p.OP50Voltage1;
                                mp.OP50Current1 = p.OP50Current1;
                                mp.OP50Frequency1 = p.OP50Frequency1;
                                mp.OP50Duty1 = p.OP50Duty1;
                                mp.OP50FanResult1 = p.OP50FanResult1;
                                mp.OP50Voltage2 = p.OP50Voltage2;
                                mp.OP50Current2 = p.OP50Current2;
                                mp.OP50Frequency2 = p.OP50Frequency2;
                                mp.OP50Duty2 = p.OP50Duty2;
                                mp.OP50FanResult2 = p.OP50FanResult2;
                                mp.OP50Presure = p.OP50Presure;
                                mp.OP50Leakage = p.OP50Leakage;
                                mp.OP50GasResult = p.OP50GasResult;
                                mp.OP50Result = p.OP50Result;
                                break;
                            case "OP60":
                                mp.OP60Code = p.OP60Code;
                                mp.OP60Result = p.OP60Result;
                                mp.OP60Picture = p.OP60Picture;
                                break;
                            case "GP12": //GP12工位
                                mp.GP12Result = p.GP12Result;
                                mp.GP12Code = p.GP12Code;
                                mp.GP12Image1 = p.GP12Image1;
                                mp.GP12Image2 = p.GP12Image2;
                                break;
                            case "JZ": // 减震工位
                                mp.JZResult = p.JZResult;
                                mp.JZImage1 = p.JZImage1;
                                mp.JZImage2 = p.JZImage2;
                                break;
                        }
                        #endregion
                        db.Production.AddOrUpdate(mp);
                        result = db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    LogTip($"{DateTime.Now.ToString("HH:mm:ss:fff")} : {e.Message}");
                }
            }
            return result;
        }

        /// <summary>
        /// 获取上一个工位的数据发送给PLC
        /// </summary>
        /// <param name="proID"></param>
        /// <param name="IsSpecialWorkstation">是否特殊工位，特殊工位传的是总成码</param>
        public static string GetLastStationPara(string proID,bool IsSpecialWorkstation)
        {
            string result = string.Empty;
            lock (locker)
            {
                try
                {
                    using (ProductionQueryContext db = new ProductionQueryContext())
                    {
                        ProduceData pro = null;

                        if (IsSpecialWorkstation)
                        {
                            pro = db.Production.Where(o => o.OP60Code == proID).FirstOrDefault();
                        }
                        else
                        {
                            pro = db.Production.Where(o => o.TrayId == proID).FirstOrDefault();
                        }

                        if (pro == null)
                        {
                            byte[] datas = new byte[16];
                            result = JsonConvert.SerializeObject(datas);
                            return result;
                        }
                        else
                        {
                            switch (Fields.Station)
                            {
                                case "OP10":
                                    byte[] datas = new byte[16];
                                    result = JsonConvert.SerializeObject(datas);
                                    break;
                                default:
                                    result = pro.LastStationData;
                                    break;
                            }
                        }
                    }
                }
                catch
                {

                }
            }
            return result;
        }




        /// <summary>
        /// 获取总成码获取工位的数据
        /// </summary>
        /// <param name="partCode"></param>
        public static ProduceData GetLastStation(string partCode)
        {

            ProduceData pro = null;
            lock (locker)
            {
                try
                {
                    using (ProductionQueryContext db = new ProductionQueryContext())
                    {
                        

                        pro = db.Production.Where(o => o.OP60Code == partCode).FirstOrDefault();
                    }
                }
                catch
                {

                }
            }
            return pro;
        }



        /// <summary>
        /// 获取载具id获取工位的数据
        /// </summary>
        /// <param name="partCode"></param>
        public static ProduceData GetStationData(string TrayId)
        {

            ProduceData pro = null;
            lock (locker)
            {
                try
                {
                    using (ProductionQueryContext db = new ProductionQueryContext())
                    {


                        pro = db.Production.Where(o => o.TrayId == TrayId).FirstOrDefault();
                    }
                }
                catch
                {

                }
            }
            return pro;
        }



        /// <summary>
        /// 获取code获取工位的数据
        /// </summary>
        /// <param name="partCode"></param>
        public static ProduceData GetCodeStationData(string QrCode)
        {

            ProduceData pro = null;
            lock (locker)
            {
                try
                {
                    using (ProductionQueryContext db = new ProductionQueryContext())
                    {


                        pro = db.Production.Where(o => o.OP10PartCode1 == QrCode ||
                                                                     o.OP10PartCode2 == QrCode ||
                                                                     o.OP10PartCode3 == QrCode ||
                                                                     o.OP10PartCode4 == QrCode ||
                                                                     o.OP20PartCode1 == QrCode ||
                                                                       o.OP20PartCode2 == QrCode ||
                                                                         o.OP20PartCode3 == QrCode ||
                                                                           o.OP20PartCode4 == QrCode ||
                                                                     o.OP30PartCode1 == QrCode ||
                                                                     o.OP30PartCode2 == QrCode ||
                                                                     o.OP30PartCode3 == QrCode ||
                                                                     o.OP30PartCode4 == QrCode ||
                                                                     o.OP60Code == QrCode).FirstOrDefault();
                    }
                }
                catch
                {

                }
            }
            return pro;
        }
       

        #endregion

        #region Log

        public static void Log(string msg) => LogTip(msg);

        public static void LogTip(string msg) => Locator.Instance.Log.LogTip(msg, Fields.Station);
        public static void LogWarning(string msg) => Locator.Instance.Log.LogWarning(msg, Fields.Station);
        public static void LogAlarm(string msg) => Locator.Instance.Log.LogAlarm(msg, Fields.Station);
        #endregion
    }
}
